package com.ushio.gougouwangguo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ushio.commonmodule.utils.JwtUtil;
import com.ushio.gougouwangguo.entity.mysql.*;
import com.ushio.gougouwangguo.entity.vo.AdventureTypeDispositionVo;
import com.ushio.gougouwangguo.entity.vo.DogCharacterVo;
import com.ushio.gougouwangguo.mapper.*;
import com.ushio.gougouwangguo.service.BattleLogService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class BattleLogServiceImpl extends ServiceImpl<BattleLogMapper, BattleLog> implements BattleLogService   {

    @Autowired
    AdventureTypeDispositionMapper adventureTypeDispositionMapper;

    @Autowired
    AdventureMapper adventureMapper;
    @Autowired
    AdventureTypeMapper adventureTypeMapper;
    @Autowired
    AdventureLogMappper adventureLogMappper;
    @Autowired
    BattleLogMapper battleLogMapper;
    @Autowired
    DogCharacterMapper dogCharacterMapper;

    @Autowired
    MonsterMapper monsterMapper;


    @Override
    public void saveTheBattleLog(List<AdventureLog> adventureLogList){
        System.out.println("======================战斗==========================");
        List<AdventureTypeDispositionVo> adventureTypeDispositionList = new ArrayList<>();
        for (AdventureLog adventureLog :adventureLogList) {

            Long adventureLogId = adventureLog.getAdventureLogId();

            AdventureTypeDisposition byId = adventureTypeDispositionMapper.selectById(adventureLog.getAdventureTypeDispositionId());

            AdventureTypeDispositionVo adventureTypeDispositionVo = AdventureTypeDispositionVo.builder().build();

            BeanUtils.copyProperties(byId,adventureTypeDispositionVo);

            adventureTypeDispositionVo.setAdventureLogId(adventureLogId);

            adventureTypeDispositionList.add(adventureTypeDispositionVo);
        }

        Long adventureId = adventureLogList.stream()
                .findFirst()
                .map(AdventureLog::getAdventureId)
                .orElse(null);

        Adventure adventure = adventureMapper.selectById(adventureId);

        DogCharacterVo dogCharacterVo = dogCharacterMapper.selectDogAttribute(adventure.getDogCharacterId());

        Integer bloodVolume = dogCharacterVo.getBloodVolume();

        for (AdventureTypeDispositionVo adventureTypeDisposition:adventureTypeDispositionList) {

            Integer monsterId = adventureTypeDisposition.getMonsterId();

            if(monsterId!=null){
                BattleLog battleLog = BattleLog.builder().build();
                battleLog.setDogBloodBar(dogCharacterVo.getBloodVolume());

                battleLog.setDogCharacterId(dogCharacterVo.getDogCharacterId());

                battleLog.setCategory(adventureId);

                battleLog.setSequence(1);

                battleLog.setBattleType((byte) 1);
                Monster monster = monsterMapper.selectById(monsterId);
                //保存战斗日志
                //存放怪物id
                battleLog.setMonsterId(monster.getMonsterId());
                //第一次谁攻击
                battleLog.setWhoIsTheAttacker((byte) 1);
                //战斗类型
                battleLog.setBattleType((byte) 2);
                //清空id
                battleLog.setBattleLogId(null);
                //狗狗血量值
                battleLog.setDogBloodBar(bloodVolume);
                //怪物血量
                battleLog.setMonsterBloodBar(monster.getMonsterBloodVolume());
                //保存id
                battleLog.setCreateUser(JwtUtil.parseUser().getUserId());

                battleLog.setAdventureLogId(adventureTypeDisposition.getAdventureLogId());
                //保存开始准备的战斗日志
                battleLogMapper.insert(battleLog);

                BattleLog battleLogOver = theBattleBegins(dogCharacterVo, monster, battleLog,bloodVolume);

                if (battleLogOver.getDogBloodBar() <= 0){
                    System.out.println("狗狗血量值为0了");
                    battleLogOver.setBattleLogId(null);
                    battleLogOver.setSequence(battleLogOver.getSequence()+1);
                    battleLogOver.setBattleType((byte) 5);
                    battleLogMapper.insert(battleLogOver);
                    adventureLogMappper.updateById(AdventureLog.builder().adventureLogId(adventureTypeDisposition.getAdventureLogId()).adventureLogType((byte) 0).build());
                    break;
                }

                bloodVolume = battleLogOver.getDogBloodBar();

                Integer treasureBoxId = adventureTypeDisposition.getTreasureBoxId();

                adventureLogMappper.updateById(AdventureLog.builder().adventureLogType((byte) 1).adventureLogId(adventureTypeDisposition.getAdventureLogId()).build());

            }
            adventureLogMappper.updateById(AdventureLog.builder().adventureLogType((byte) 1).adventureLogId(adventureTypeDisposition.getAdventureLogId()).build());
        }




    }

    public BattleLog theBattleBegins(DogCharacterVo dogCharacterVo, Monster monster, BattleLog battleLog,Integer dogBloodBar){


        Long userId = JwtUtil.parseUser().getUserId();

        Integer sequence = battleLog.getSequence();

        Long category = battleLog.getCategory();


        Integer bloodVolume = dogBloodBar;

        Integer monsterBloodVolume = monster.getMonsterBloodVolume();

        Integer monsterAttackPower = monster.getMonsterAttackPower();

        do {
            Date adventureTime = new Date();
            //查询保存的怪物属性攻击力以及血量
            BattleLog battleLogMax = battleLogMapper.selectOne(new LambdaQueryWrapper<BattleLog>()
                    .eq(BattleLog::getCreateUser, userId)
                    .eq(BattleLog::getCategory,category)
                    .eq(BattleLog::getAdventureLogId,battleLog.getAdventureLogId())
                    .orderByDesc(BattleLog::getSequence)
                    .last("LIMIT 1"));
            //武器攻击力
            Integer wqAttribute = dogCharacterVo.getWqAttribute();
            //防具防御力
            Integer fjAttribute = dogCharacterVo.getFjAttribute();
            //鞋子防御力
            Integer xzAttribute = dogCharacterVo.getXzAttribute();
            //判断谁攻击
            byte whoIsTheAttacker = battleLogMax.getWhoIsTheAttacker();
            //狗狗攻击
            if (whoIsTheAttacker == 1) {
                System.out.println("==================战斗开始!!!!================");
//                        //怪物原本血量
//                        battleLog.setVictimBloodBarInitiate(victimBloodBarLater);
                //怪物剩余血量
                monsterBloodVolume = monsterBloodVolume - wqAttribute;
                //受害者被攻击后的血量
                if (monsterBloodVolume<=0){
                    battleLog.setMonsterBloodBar(0);
                }else{
                    battleLog.setMonsterBloodBar(monsterBloodVolume);
                }
                //伤害值
                battleLog.setDamageValue(wqAttribute);
                //下次换怪兽攻击
                battleLog.setWhoIsTheAttacker((byte) 2);
            }else if (whoIsTheAttacker == 2)  {

                //狗狗剩余血量
                bloodVolume = bloodVolume - (monsterAttackPower-fjAttribute-xzAttribute);
                //被攻击后血量
                if (bloodVolume<=0){
                    battleLog.setDogBloodBar(0);
                }else{
                    battleLog.setDogBloodBar(bloodVolume);
                }
                //伤害值
                battleLog.setDamageValue(monsterAttackPower-fjAttribute-xzAttribute);
                //下次换狗狗攻击
                battleLog.setWhoIsTheAttacker((byte) 1);
            }

            battleLog.setBattleLogId(null);
            battleLog.setSequence(++sequence);
            battleLogMapper.insert(battleLog);

        } while (bloodVolume > 0 && monsterBloodVolume > 0);

        battleLog.setBattleType((byte) 3);
        battleLog.setBattleLogId(null);
        battleLog.setSequence(++sequence);
        battleLogMapper.insert(battleLog);

        return battleLog;
    }


}
